<template>
	<view class="pay" v-if="xfId == 4">
		<view class="smPay">
			<view class="pay_text">
				联盟矩阵服务1年(续费)
			</view>
			<view class="pay_text1">
				200元/年
			</view>
			<view class="pay_num">
				<view class="num_text1" @click="add(1)">
					<image src="../../static/jia/jian.png" mode=""></image>
				</view>
				<view class="num_text2">{{value}}</view>
				<view class="num_text3" @click="add(2)">
					<image src="../../static/jia/jia.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="pay_hj">
			合计： ¥{{xfmoneyAll}}
		</view>
		<view class="pay_btn" @click="goShip">
			<image src="../../static/jia/weixin.png" mode="widthFix"></image>
			<text>微信支付</text>
		</view>
	</view>
	<view class="pay" v-else>
		<view class="smPay">
			<view class="pay_text">
				联盟矩阵服务1年(开通)
			</view>
			<view class="pay_text1">
				{{money}}元/{{year}}天
			</view>
			<view class="pay_num">
				<view class="num_text1" @click="add(1)">
					<image src="../../static/jia/jian.png" mode=""></image>
				</view>
				<view class="num_text2">{{value}}</view>
				<view class="num_text3" @click="add(2)">
					<image src="../../static/jia/jia.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="pay_hj">
			合计： ¥{{moneyOne}}
		</view>
		<view class="pay_btn" @click="goShip">
			<image src="../../static/jia/weixin.png" mode="widthFix"></image>
			<text>微信支付</text>
		</view>
	</view>
</template>

<script>
	import wx from "../../wxjdk/jdk.js"
	import {
		wxpay,
		status,
		gongneng
	} from "../../api/pay/index.js"
	export default {
		data() {
			return {
				value: 1,
				money: 350,
				moneyOne: 350,
				shipId: 3,
				xfId: null,
				xfmoney: 200,
				xfmoneyAll: 200,
				year: "365",
				istry: 1,
				orderInfo: {},
				orderid: ""
			};
		},
		onLoad(options) {
			gongneng({
				type: 3
			}).then(res => {
				console.log(res)
				if (res.data.is_event == 0) {
					this.money = res.data.price
				} else {
					this.money = res.data.event_price
					this.year = res.data.day
				}
			})
		},
		onShow() {
			if (this.orderid !== '') {
				status({
					order_id: this.orderid
				}).then(res => {
					if (res.data.status == 1) {

						uni.redirectTo({
							url: "/pages/trusteeship/trusteeship?id=3"
						});
						window.clearInterval(t)
					}
				})
				// let t=setTimeout(function(){
				// 	if(this.orderid){
				// 		status({order_id:this.orderid}).then(res=>{
				// 			if(res.data.status==1){

				// 				uni.redirectTo({
				// 				    url: "/pages/trusteeship/trusteeship?id=3"
				// 				});
				// 				 window.clearInterval(t)
				// 			}
				// 		})
				// 	}
				// },1000)

			}

		},
		methods: {
			oldPeople(res) {//微信内置浏览器调起支付
				let this_ = this;
				WeixinJSBridge.invoke(
					"getBrandWCPayRequest", {
						appId: res.appid,
						timeStamp: res
						.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
						package: res.packageValue, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
						signType: "MD5", // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
						paySign: res.paySign // 支付签名
					},
					function(res) {
						if (res.err_msg == "get_brand_wcpay_request:ok") {
							this_.$router.push("/paySuccess");
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						} else if (res.err_msg == "get_brand_wcpay_request:fail") {
							// 支付失败
							this_.$router.push("/paymentFailure");
						}
					}
				);
			},

			add(e) {//价格监听
				console.log(e, '加减')
				if (this.xfId != 4) {
					if (e == 1) {
						if (this.value != 1) {
							this.value--
							this.moneyOne = this.value * this.money
						} else {
							return false
						}
					} else {
						this.value++
						this.moneyOne = this.value * this.money
					}
				} else {
					if (e == 1) {
						if (this.value != 1) {
							this.value--
							this.xfmoneyAll = this.value * this.xfmoney
						} else {
							return false
						}
					} else {
						this.value++
						this.xfmoneyAll = this.value * this.xfmoney
					}
				}
			},
			goShip() {
				let _this = this
				let shop = uni.getStorageSync("shop_id")
				let arr = {
					merchantId: this.vuex_merchantId, //商户id
					shop_id: shop,
					type: 3,
					num: this.value,
					total_money: this.moneyOne,
					is_try: this.istry
				}
				wxpay({
					...arr
				}).then(res => {
					console.log(res)
					if (res.code == 0) {
						this.orderInfo = res.data
						this.orderid = res.data.log_id
						
						// uni.requestPayment({
						// 	provider: 'wxpay',
						// 	orderInfo: _this.orderInfo,
						// 	success: function(res) {
						// 		console.log('success:' + JSON.stringify(res));
						// 		status({order_id:_this.orderid}).then(res=>{
						// 			if(res.data.status==1){
						// 				uni.redirectTo({
						// 				    url: "/pages/trusteeship/trusteeship?id=3"
						// 				});
						// 			}else{
						// 				 uni.showToast({
						// 				           title: "状态异常请重试!"
						// 				               })
						// 							   uni.redirectTo({
						// 							       url: "/pages/index/index"
						// 							   });
						// 			}
						// 		})
						// 		},
						// 	fail: function(err) {
						// 		console.log('fail:' + JSON.stringify(err));
						// 		uni.redirectTo({
						// 		    url: "/pages/index/index"
						// 		})
						// 		}
						// });
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	uni-page-body {

		background-color: #fff;
	}

	.smPay {
		margin-top: 70rpx;
		margin-left: 30rpx;
		width: 690rpx;
		height: 100rpx;

		.pay_text {
			font-size: 30rpx;
			color: #333333;
		}

		.pay_text1 {
			font-size: 36rpx;
			font-weight: bold;
			margin-top: 6rpx;
		}

		.pay_num {
			margin-left: 530rpx;
			margin-top: -70rpx;
			display: flex;

			.num_text1 {
				border: 2rpx solid #e5e5e5;
				width: 44rpx;
				height: 44rpx;

				image {
					width: 100%;
					height: 100%;

				}
			}

			.num_text2 {
				width: 64rpx;
				height: 44rpx;
				border: 2rpx solid #e5e5e5;
				text-align: center;
				font-size: 30rpx;
				font-weight: bold;

			}

			.num_text3 {
				border: 2rpx solid #e5e5e5;
				width: 44rpx;
				height: 44rpx;

				image {
					width: 100%;
					height: 100%;

				}
			}
		}
	}

	.pay_hj {
		margin-top: 60rpx;
		margin-left: 30rpx;
		width: 690rpx;
		height: 100rpx;
		background-color: #f2f2f2;
		text-align: right;
		line-height: 100rpx;
		font-size: 26rpx;
		padding-right: 30rpx;
		border-radius: 6rpx;
	}

	.pay_btn {
		position: absolute;
		top: 85%;
		width: 600rpx;
		height: 100rpx;
		margin-left: 75rpx;
		border-radius: 52rpx;
		background-color: #28c445;

		image {
			width: 42rpx;
			height: 34rpx;
			position: absolute;
			left: 204rpx;
			top: 25rpx;
		}

		text {
			color: #fff;
			font-size: 34rpx;
			font-weight: bold;
			position: absolute;
			top: 22rpx;
			left: 260rpx;
		}
	}
</style>
